package com.handinglian.ai.system.controller;

import cn.hutool.core.util.StrUtil;
import com.apidoc.annotation.Api;
import com.handinglian.ai.common.dto.CommonPage;
import com.handinglian.ai.common.dto.ResultData;
import com.handinglian.ai.common.shiro.Principal;
import com.handinglian.ai.system.dto.HomePageDto;
import com.handinglian.ai.system.dto.UserInfoDetailDto;
import com.handinglian.ai.system.dto.UserInfoDto;
import com.handinglian.ai.system.entity.UserInfo;
import com.handinglian.ai.system.param.UpdatePasswordParam;
import com.handinglian.ai.system.param.UserCreateParam;
import com.handinglian.ai.system.param.UserRecoverParam;
import com.handinglian.ai.system.param.UserUpdateParam;
import com.handinglian.ai.system.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Api("用户")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 创建用户
     */
    @PostMapping(value = "/create")
    public ResultData create(@RequestBody UserCreateParam userCreateParam) {
        UserInfo userInfo = userInfoService.getInvalid(userCreateParam.getAccount());
        if (Objects.nonNull(userInfo)){
            return ResultData.existsInHistory();
        }
        return ResultData.success(userInfoService.create(userCreateParam));
    }

    /**
     * 从删除列表中恢复用户
     */
    @PutMapping(value = "/recover")
    public ResultData recover(@RequestBody UserRecoverParam userRecoverParam) {
        int amount = userInfoService.recover(userRecoverParam.getAccount());
        return ResultData.success(amount);
    }

    /**
     * 删除用户
     */
    @DeleteMapping(value = "/delete")
    public ResultData delete(Integer id) {
        int amount = userInfoService.delete(id);
        return ResultData.success(amount);
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping(value = "/deleteBatch")
    public ResultData deleteBatch(String id) {
        int amount = userInfoService.deleteBatch(id);
        return ResultData.success(amount);
    }

    /**
     * 更新用户
     */
    @PutMapping(value = "/update")
    public ResultData update(@RequestBody UserUpdateParam userUpdateParam) {
        int amount = userInfoService.update(userUpdateParam);
        return ResultData.success(amount);
    }

    /**
     * 获取用户列表
     */
    @GetMapping(value = "/listPage")
    public ResultData<CommonPage<UserInfoDto>> listPage(String keyword, String loginPermission, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return ResultData.success(userInfoService.listPage(keyword, loginPermission, pageNum, pageSize));
    }

    /**
     * 重置密码
     */
    @GetMapping(value = "/resetPassword")
    public ResultData resetPassword(Integer id) {
        return ResultData.success(userInfoService.resetPassword(id));
    }

    /**
     * 获取用户基本信息
     */
    @GetMapping(value = "/get")
    public ResultData<UserInfoDto> get(String account) {
        return ResultData.success(userInfoService.getUserInfoDto(account));
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public ResultData updatePassword(@RequestBody UpdatePasswordParam updatePasswordParam) {
        return ResultData.success(userInfoService.updatePassword(updatePasswordParam));
    }

    /**
     * 查询用户详情
     */
    @GetMapping(value = "/getDetail")
    public ResultData<UserInfoDetailDto> getDetail(String account) {
        return ResultData.success(userInfoService.getDetail(account));
    }

    /**
     * 更新用户状态
     */
    @GetMapping(value = "/updateLoginPermission")
    public ResultData updateLoginPermission(Integer loginPermission, Integer id) {
        return ResultData.success(userInfoService.update(loginPermission, id));
    }

    /**
     * 获取首页数据
     */
    @GetMapping(value = "/getHomePage")
    public ResultData<HomePageDto> getHomePage() {
        return ResultData.success(userInfoService.getHomePageDate());
    }
}
